package com.apache.log.db.impl;

import com.apache.audit.entity.LogModel;
import com.apache.audit.entity.LogModelDetail;
import com.apache.audit.entity.LogOperation;
import com.apache.audit.entity.LogOperationDetail;
import com.apache.database.model.Page;
import com.apache.log.db.DatabaseOperate;
import com.apache.log.db.DbBaseDao;
import com.apache.tools.StrUtil;
import com.apache.tools.UUIDUtil;

import java.util.List;
import java.util.Map;

/**
 * @description: 数据库统一操作DAO接口实现
 * @author: Hou Dayu
 * @date: Created in 2020/11/18
 */
public class DefDbDaoImpl implements DbBaseDao {

    private String datasourceKey;

    public DefDbDaoImpl(){
        this.datasourceKey = "plateform";
    }

    public DefDbDaoImpl(String datasourceKey){
        this.datasourceKey = datasourceKey;
    }


    @Override
    public boolean insert(Map<String, Object> param) {
        Object obj = param.get("value");
        if(obj instanceof LogModel){
            StringBuilder sql = new StringBuilder();
            sql.append("insert into uct_log_model (model_id,model_cname,model_ename,model_bussiness,model_sysname,model_remark) values(");
            LogModel logModel = (LogModel)obj;
            sql.append("'"+logModel.getModelId()+"','"+logModel.getModelCname()+"','"+logModel.getModelEname()+"','"+logModel.getModelBussiness()+"',");
            sql.append("'"+logModel.getModelSysname()+"','"+logModel.getModelRemark()+"')");
            return DatabaseOperate.getInstance().execute(datasourceKey,sql.toString());
        } else if(obj instanceof LogModelDetail){
            StringBuilder sql = new StringBuilder();
            sql.append("insert into uct_log_model_detail (detail_id,model_id,column_cname,column_ename,primary_key) values(");
            LogModelDetail logModelDetail = (LogModelDetail)obj;
            sql.append("'"+logModelDetail.getDetailId()+"','"+logModelDetail.getModelId()+"','"+logModelDetail.getColumnCname()+"',");
            sql.append("'"+logModelDetail.getColumnEname()+"','"+logModelDetail.getPrimaryKey()+"')");
            return DatabaseOperate.getInstance().execute(datasourceKey,sql.toString());
        } else if(obj instanceof LogOperation){
            StringBuilder sql = new StringBuilder();
            sql.append("insert into  uct_log_operation (log_id,model_id,log_type,org_id,org_ename,created_time,create_user,log_ip,primary_key) values (");
            LogOperation logOperation = (LogOperation)obj;
            sql.append("'"+logOperation.getLogId()+"','"+logOperation.getModelId()+"','"+logOperation.getLogType()+"','"+logOperation.getOrgId()+"',");
            sql.append("'"+logOperation.getOrgEname()+"','"+logOperation.getCreatedTime()+"','"+logOperation.getCreateUser()+"',");
            sql.append("'"+logOperation.getLogIp()+"','"+logOperation.getPrimaryKey()+"')");
            Object detail = param.get("details");
            if(detail==null) {
                return DatabaseOperate.getInstance().execute(datasourceKey, sql.toString());
            } else {
                List<LogOperationDetail> list = (List)detail;
                String sqlStr[] = new String[list.size()+1];
                sqlStr[0]=sql.toString();
                int count =list.size();
                for(int i=0;i<count;i++){
                    LogOperationDetail ldetail = list.get(i);
                    StringBuilder sql2 = new StringBuilder("INSERT INTO uct_log_operation_detail (detail_id,log_id,property_name,property_ename,property_value,primary_key) values(");
                    sql2.append("'"+ UUIDUtil.getPrimaryKey() +"','"+logOperation.getLogId()+"','"+ldetail.getPropertyName()+"',");
                    sql2.append("'"+ldetail.getPropertyEname()+"','"+ldetail.getPropertyValue()+"','"+ldetail.getPrimaryKey()+"')");
                    sqlStr[i+1]=sql.toString();
                }
                return DatabaseOperate.getInstance().execute(datasourceKey, sqlStr);
            }
        }
        return false;
    }

    @Override
    public boolean edit(Map<String, Object> param) {
        Object obj = param.get("value");
        StringBuilder sql = new StringBuilder();
        if(obj instanceof LogModel){
            LogModel logModel = (LogModel)obj;
            sql.append("UPDATE uct_log_model set ");
            sql.append("model_cname='"+logModel.getModelCname()+"',");
            sql.append("model_bussiness='"+logModel.getModelBussiness()+"',");
            sql.append("model_sysname='"+logModel.getModelSysname()+"',");
            sql.append("model_remark='"+logModel.getModelRemark()+"' ");
            sql.append("WHERE model_id ='"+logModel.getModelId()+"'");
        } else if(obj instanceof LogModelDetail){
            LogModelDetail logModelDetail = (LogModelDetail)obj;
            sql.append("UPDATE uct_log_model_detail set ");
            sql.append("model_id='"+logModelDetail.getModelId()+"',");
            sql.append("column_cname='"+logModelDetail.getColumnCname()+"',");
            sql.append("primary_key='"+logModelDetail.getPrimaryKey()+"' ");
            sql.append("WHERE detail_id ='"+logModelDetail.getDetailId()+"'");
        }
        return DatabaseOperate.getInstance().execute(datasourceKey,sql.toString());
    }

    @Override
    public boolean delete(Map<String, Object> param) {
        String sql = String.valueOf(param.get("sqlStr"));
        return DatabaseOperate.getInstance().execute(datasourceKey,sql);
    }

    @Override
    public Object selectById(Map<String, Object> param) {
        String sql = String.valueOf(param.get("sqlStr"));
        return DatabaseOperate.getInstance().selectInfo(datasourceKey,sql);
    }

    @Override
    public Page pageSelect(Map<String, Object> param, int pageNo, int pageSize) {
        String sql = String.valueOf(param.get("sqlStr"));
        String orderby = String.valueOf(param.get("orderby"));
        Map<String,Object> data = DatabaseOperate.getInstance().selectPage(datasourceKey,sql,orderby,pageNo,pageSize);
        Page page = new Page();
        page.setPageNeeded(pageNo);
        page.setPageSize(pageSize);
        page.setCount(Integer.parseInt(StrUtil.doNull(String.valueOf(data.get("total")),"0")));
        page.setPageObjects((List)data.get("rows"));
        return page;
    }

    @Override
    public List select(Map<String, Object> param) {
        String sql = String.valueOf(param.get("sqlStr"));
        String orderby = String.valueOf(param.get("orderby"));
        Map<String,Object> data = DatabaseOperate.getInstance().selectPage(datasourceKey,sql,orderby,0,0);
        return (List)data.get("rows");
    }
}
